// 写路由守卫和路由跳转的文件
// import { ItemChildrenWrap } from 'antd-mobile/es/components/dropdown/item';
import React, { PureComponent } from 'react';
//, withRouter
import './index.scss'
import { Redirect, Route } from 'react-router-dom';
// import Main from '../pages/main';
class RouterView extends PureComponent {
    render() {
        console.log(this.props);
        const { routes, location } = this.props;
        //当前页面链接
        const { pathname } = location;

        //路由守卫,需要获取 登录状态
        const isLogin = false;  //登录状态可以保存到 redux 、缓存中

        //需要找到匹配的路由
        const targetRouterConfig = routes.find(item => {
            return item.path.replace(/\s*/g, "") === pathname ||
                item.name === pathname.substr(0, pathname.lastIndexOf("/"))
        })
        console.log(targetRouterConfig);
        const { component, path } = targetRouterConfig;

        if (targetRouterConfig && !targetRouterConfig.auth && !isLogin) {
            return <Route path={path} component={component} />
        }

        if (isLogin) {//已经登录
            return <Route path={path} component={component} />
        } else {
            if (targetRouterConfig && targetRouterConfig.auth) {
                return <Redirect to="/login" />
            }
        }
    }
}

export default RouterView;